import {render, getCookie, dataHandle} from '../initTemplate'
import requestApi from '../requestApi';
import 'src/erm/sscrppublic/common/components/globalPresetVar';
import logs from 'src/erm/sscrppublic/common/utils/logs';
import { debuglog } from 'util';
import {setPageStatus} from 'src/erm/sscrppublic/common/utils/statusController';
import { cardCache,toast,base } from 'nc-lightapp-front';
import {updataFormData, updatacardTableData} from 'src/erm/sscrppublic/common/utils/changePageData';
import attachments from 'uap/common/components/getAttachmentInfo';
const { getAttachments } = attachments;

const {NCMessage} = base;
let { updateCache } = cardCache;
let debugLogs = new logs();
export default function () {
    let props = this.props;
    let _this = this;
    let pageStatus = window.presetVar.pagestatus==''||window.presetVar.pagestatus==undefined?props.getUrlParam('status'):window.presetVar.pagestatus;
    let appcode = (props.getUrlParam("appcode")==undefined||props.getUrlParam("appcode")=='')?props.getSearchParam("c"):props.getUrlParam("appcode");
    let pagecode =  (props.getUrlParam("pagecode")==undefined||props.getUrlParam("pagecode")=='')?props.getSearchParam("p"):props.getUrlParam("pagecode");
    render.call(this, props.meta.getMeta(),window.presetVar.buttonTpl,pageStatus);
    let viewbilldata = {};
    viewbilldata.openbillid = props.getUrlParam("id")||props.getUrlParam("billid");
    viewbilldata.tradetype = props.getUrlParam("tradetype")||window.presetVar.tradetype;
    viewbilldata.uid = getCookie("userid");//"1001Z3100000000000AE";这里以后从session中获取
    viewbilldata.pagecode = pagecode;
    viewbilldata.appcode = appcode;
    requestApi.viewBill({
        data:viewbilldata,
        success:(data) =>{
            props.form.setAllFormValue({["head"]:data.head["head"]});
            window.presetVar.reloadHead = data.head["head"];
            //设置表体数据
            Object.values(window.presetVar.body).forEach((body) =>{
                if(data.bodys[body]){
                    const data_list = requestApi.filterDatas(data.bodys[body])
                    props.cardTable.setTableData(body, data_list);
                } else {
                    props.cardTable.setTableData(body, {areacode:body, rows:[]});
                }
            });
            dataHandle.call(_this,window.presetVar.reloadHead,props,pageStatus);

            let ls_bodys = [];
            Object.values(window.presetVar.body).forEach((body)=>{
                ls_bodys.push(body);
            });
            setPageStatus(props, props.meta.getMeta(), ['head'], ls_bodys, 'browse');
            // BTM 整体设置页面状态
            //}pa
            let iscostshare = props.form.getFormItemsValue("head",['iscostshare'])==null?"":props.form.getFormItemsValue("head",['iscostshare'])[0].value
            if(iscostshare== true){
                let hideTables = _this.state.hideTables
                let index = hideTables.findIndex(item => item === 'er_cshare_detail')
                if (index > -1) {
                    hideTables.splice(index, 1)
                    _this.setState({ hideTables: hideTables });
                }
            }
            requestApi.setMetasInit(props.meta.getMeta(),props,data);
            if(data.userjson && JSON.parse(data.userjson)){
                let djlx = JSON.parse(data.userjson);
                if(djlx && djlx.ISCONTRAST && djlx.jkyjye && djlx.jkyjye >0){//是否冲借款
                    window.presetVar["ISCONTRAST"]=djlx.ISCONTRAST;
                }
                if(djlx && djlx.IS_MACTRL){//是否冲借款
                    window.presetVar["IS_MACTRL"]=djlx.IS_MACTRL;
                }
                _this.pubMessage.refreshSuccess();

                let multiLang = props.MutiInit.getIntl(2011); //this.moduleId
                let id =   props.getUrlParam("id")||props.getUrlParam("billid") //window.presetVar.id || this.state.billId;
                let fileman;
                id = id == "" ? 0 : id;
                fileman = multiLang && multiLang.get('201102BCLF-0040');
                getAttachments(id).then(lists => {
                    this.setState({
                        groupLists: lists
                    }, () => {
                        let filenum = this.state.groupLists;
    
                        if (filenum.length == 0) {
                            filenum = 0;
                        }
                        window.presetVar.buttonTpl.button.find((items) => {
                            if (items.key == 'fileManager') {
                                items.title = fileman + ' ' + filenum + ' ';
                                props.button.setButtonTitle(items.key, items.title);
                                return true;
                            }
                        });
                    })
                })
               
            }
        }
    });
}
